<!-- 储存职位控件 -->
<template>
  <!-- 标准职位名称 -->
  <PositionStandName
    v-if="code === 'POSITON_STAND_NAME'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </PositionStandName>
  <!-- 职位名称 -->
  <PositionName
    v-if="code === 'POSITON_NAME'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </PositionName>
  <!-- 标准职位编码 -->
  <PositionStandCode
    v-if="code === 'POSITON_STAND_CODE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </PositionStandCode>
  <!-- 职位编码 -->
  <PositionCode
    v-if="code === 'POSITON_CODE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </PositionCode>
  <!-- 职位类别 -->
  <PositionType
    v-if="code === 'POSITON_TYPE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </PositionType>
  <!-- 职位层级 -->
  <PositionLevel
    v-if="code === 'POSITON_LEVEL'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </PositionLevel>
  <!-- 是否特殊工种 -->
  <PositionSpecialProfession
    v-if="code === 'POSITON_SPECIAL_PROFESSION'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </PositionSpecialProfession>
  <!-- 是否关键职位 -->
  <PositionKeyJob
    v-if="code === 'POSITON_KEY_JOB'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </PositionKeyJob>
  <!-- 职位使命 -->
  <PositionMission
    v-if="code === 'POSITON_MISSION'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </PositionMission>
  <!-- 所属部门 -->
  <PositionDepartment
    v-if="code === 'POSITON_DEPARTMENT'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </PositionDepartment>
  <!-- 关联职务 -->
  <PositionRelated
    v-if="code === 'POSITON_RELATED'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </PositionRelated>
  <!-- 关联职务 -->
  <PositionStandRelated
    v-if="code === 'POSITON_STAND_RELATED'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </PositionStandRelated>
  <!-- 选择职位说明书模板 -->
  <PositionTemplate
    v-if="code === 'POSITON_TEMPLATE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </PositionTemplate>
  <!-- 职位序列 -->
  <PositionSequence
    v-if="code === 'POSITON_SEQUENCE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </PositionSequence>
  <!-- 职位序列 -->
  <PositionRange
    v-if="code === 'POSITON_RANGE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </PositionRange>
  <!-- 是否设为标准职位 -->
  <PositionSetStandPosition
    v-if="code === 'POSITON_SET_STAND_POSITION'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </PositionSetStandPosition>

  <!-- 入职时间 -->
  <PositionSetStandPosition
    v-if="code === 'POSITON_ENTRY_DATE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </PositionSetStandPosition>
</template>

<script setup lang="ts">
const props = withDefaults(
  //参数说明
  defineProps<{
    componentType: string;
    code: string;
    props: { [propName: string]: any };
    name?: string;
    required: boolean;
    values: any;
    disabled?: boolean;
    regex?: {
      [name: string]: {
        role: any;
        name: any;
      };
    };
  }>(),
  {}
);

// console.log(props);

const emit = defineEmits(['update:values']);
//复制一份props
const getProps = computed(() => {
  return props.props;
});

const validator = (value: any, callback: any) => {
  const { componentType, name, values, required, regex } = props;
  if (componentType === 'TEXT') {
    //单行输入框
    const { validate } = getProps.value;
    if (required && !values[0]) return callback('请输入' + name);
    if (!validate) return callback();

    if (regex) {
      const roleItem = regex[validate];
      const checkVal = roleItem.role.test(values[0]);
      if (!checkVal) {
        return callback(new Error('请输入正确的' + roleItem.name));
      }
    }
  } else if (componentType === 'TEXT_AREA') {
    //多行输入框
    if (required && !values[0]) return callback(`请输入${name}`);
  } else if (componentType === 'NUMBER') {
    if (required && !values[0]) return callback(`请输入${name}`);
  } else if (componentType === 'DATE' || componentType === 'DATE_RANGE') {
    if (required && !values[0]) return callback(`请选择日期`);
  }

  callback();
};
//当value只用到第一个绑定这个参数
const oneValue = computed({
  get() {
    return props.values;
  },
  set(val) {
    emit('update:values', val);
  }
});

// 多选双向绑定
const checkGroupValue = computed({
  get() {
    return props.values;
  },
  set(val) {
    emit('update:values', val);
  }
});
</script>

<style></style>
